import Observer from "./observer";
import Compiler from "./compiler";

/**
 * 最后一步定义Vue整合类，传入选项式options
 */
export default class Vue {
    constructor(options) {
        this.$el = document.querySelector(options.el);
        this.$data = options.data || {};

        this._proxyData(this.$data);
        this._proxyMethods(options.methods);
        
        new Observer(this.$data);
        new Compiler(this);
    }

    _proxyData(data) {
        Object.keys(data).forEach((key) => {
            Object.defineProperty(this, key, {
                set(newValue) {
                    data[key] = newValue;
                },

                get() {
                    return data[key];
                }
            })
        })
    }

    _proxyMethods(methods) {
        if (methods && typeof methods === 'object') {
            Object.keys(methods).forEach((key) => {
                this[key] = methods[key];
            })
        }
    }
}

// 挂载到window下，可以直接使用Vue类
window.Vue = Vue;